import * as Minio from "minio";


function getclient() {
  var minioClient = new Minio.Client({
    endPoint: "192.168.11.250",
    port: 19000,
    useSSL: false,
    accessKey: "vfUn1eV8ncATGNQv",
    secretKey: "C6upU3CnucKuw7suY4wTfHF8AhSeiAet"
  });
  return minioClient;
}

export async function uploadfile(file) {
  var metaData = {
    "Content-Type": "application/octet-stream",
    "X-Amz-Meta-Testing": 1234,
    example: 5678
  };
  var fileName=`${new Date().getTime()}_${file && file.filename}`;
  var minioClient = await getclient();
  // Using fPutObject API upload your file to the bucket europetrip.
  return new Promise((resolve, reject) => {
    fileName

    minioClient.fPutObject("wiki",
      fileName,
      file?.data,
      metaData, function(err, etag) {
        if (err) {
          reject(err);
          return console.log(err);
        }
        console.log("File uploaded successfully.");

        resolve({
         ...etag,
         fileName
        });
      });
  });

}

interface tainfo {
  etag: String
  versionId: String
}
export async function getFile(filename) {
  var minioClient = await getclient();
  // Using fPutObject API upload your file to the bucket europetrip.
  return new Promise((resolve, reject) => {
    minioClient.getObject('wiki',filename,(err,aaa)=>{
      if (err) {
        reject(err);
        return console.log(err);
      }
      resolve(aaa)
    })
  });

}
